package com.parking.mgr.useraccount.interceptor;

import com.parking.mgr.common.annotion.PermissionUrl;
import com.parking.mgr.useraccount.facede.BasicMenuServiceFacede;
import com.parking.mgr.useraccount.facede.UserServiceFacede;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @ClassName PermissionUrlInterceptor
 * @Description 权限URL检验拦截器
 * @Auther: zhijianpu
 * @Date: 2023/6/13
 */
@Log4j2
@Component
public class PermissionUrlInterceptor implements HandlerInterceptor {

    @Resource
    private UserServiceFacede userServiceFacede;

    @Resource
    private BasicMenuServiceFacede basicMenuServiceFacede;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            final HandlerMethod handlerMethod = (HandlerMethod) handler;
            final Class<?> clazz = handlerMethod.getBeanType();
            final Method method = handlerMethod.getMethod();

            // 判断PermissionUrl注解是标记在所调用的方法上还是在其类上
            if (clazz.isAnnotationPresent(PermissionUrl.class) || method.isAnnotationPresent(PermissionUrl.class)) {
                String requestURI = request.getRequestURI();
                log.info("请求URI: " + requestURI);

            }
        }
        return true;
    }
}
